﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class PurchaseWeek
    {

        public static DataTable listWithCompanyAndYear(int companyId, int year)
        {
            return DbHelper.ExecuteDataTable("p_purchaseWeek_listWithCompanyAndYear", companyId, year);
        }

        /// <summary>
        /// 初始化企业周报价体系
        /// </summary>
        /// <param name="companyId">企业编号</param>
        /// <param name="year">年份</param>
        /// <param name="createAdminId">创建人编号</param>
        /// <returns>False表示该企业周报价体系已初始化</returns>
        public static bool insert(int companyId, int year, int createAdminId)
        {
            object o = DbHelper.ExecuteScalar("p_purchaseWeek_insert", companyId, year, createAdminId);
            if (o!=DBNull.Value)
            {
                return Convert.ToBoolean(o);
            }
            return false;
        }

        public static PurchaseWeekEntity getById(int bpwdid)
        {
            PurchaseWeekEntity entity = null;
            DataRow dr = DbHelper.ExecuteDataRow("p_purchaseWeek_getById", bpwdid);
            if (dr!=null)
            {
                entity = new PurchaseWeekEntity();
                if (dr[0] != DBNull.Value)
                    entity.Bpwid = int.Parse(dr["bpwid"].ToString());
                if(dr[1]!=DBNull.Value)
                    entity.CompanyId=Convert.ToInt32(dr["companyId"]);
                if (dr[2] != DBNull.Value)
                    entity.StartTime = Convert.ToDateTime(dr["startTime"]);
                if (dr[3] != DBNull.Value)
                    entity.EndTime = Convert.ToDateTime(dr["endTime"]);
                if (dr[4] != DBNull.Value)
                    entity.InputDt = Convert.ToDateTime(dr["inputDt"]);
                if (dr[5] != DBNull.Value)
                    entity.Status = dr["status"].ToString();
                if (dr[6] != DBNull.Value)
                    entity.UpdatestatusDt = Convert.ToDateTime(dr["updatestatusDt"]);
                if (dr[7] != DBNull.Value)
                    entity.CreateAdminId = Convert.ToInt32(dr["createAdminId"]);
                if (dr[8] != DBNull.Value)
                    entity.AuditAdminId = Convert.ToInt32(dr["auditAdminId"]);
                if (dr[9] != DBNull.Value)
                    entity.Week = Convert.ToInt32(dr["week"]);
                if (dr[10] != DBNull.Value)
                    entity.Year = Convert.ToInt32(dr["year"]);
            }

            return entity;
        }

        public static void updateStateByAudit(int bpwdid, int adminId,string status)
        {
            DbHelper.ExecuteNonQuery("p_purchaseWeek_updateStateByAudit", bpwdid, adminId, status);
        }
    }
}
